﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HandBook.Common;
using HandBook.Data;
using Telerik.Web.UI;

public partial class Admin_NewsDetails_Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["NewsID"] == null)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Insert;
            Button3.Text = "Insert";
        }
        else
        {
            DetailsView1.DefaultMode = DetailsViewMode.Edit;
            Button3.Text = "Update";
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Button3.Text == "Update")
        {
            UpdateNews();
            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
        }
        else if (Button3.Text == "Insert")
        {
            InsertNews();
            ScriptManager.RegisterStartupScript(this, GetType(), "closeWnd", "CloseWnd();", true);
        }
    }
    private void UpdateNews() 
    {
        int newsID = Convert.ToInt32(Request.Params["NewsID"].ToString());
        var newTemp = HandBookData.EatDrinkDB.News.Single(n => n.NewsID == newsID);

        newTemp.NewsStyleID = Convert.ToInt32((DetailsView1.FindControl("ddlNewsStyleID") as DropDownList).SelectedValue);
        newTemp.NewsTitle = (DetailsView1.FindControl("txtTitle") as TextBox).Text.Trim();
        newTemp.NewsShort = (DetailsView1.FindControl("RadEditorShort") as RadEditor).Content.Trim();
        newTemp.NewsLong = (DetailsView1.FindControl("RadEditorLong") as RadEditor).Content.Trim();
        string fileUpload = (DetailsView1.FindControl("FileUpload1") as FileUpload).FileName.Trim();
        if (!fileUpload.Equals(""))
        {
            string file = HandBook.Common.FileManager.GetFileName(fileUpload);
            string path = Server.MapPath("~/Images/News/" + file);
            (DetailsView1.FindControl("FileUpload1") as FileUpload).SaveAs(path);
            newTemp.NewsImageName = file;
        }
        newTemp.NewsPath = (DetailsView1.FindControl("txtPath") as TextBox).Text.Trim();
        newTemp.IsShowHomePage = Convert.ToBoolean((DetailsView1.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());
        HandBookData.EatDrinkDB.SubmitChanges();
    }
    private void InsertNews() 
    {
        New newTemp = new New();
        newTemp.NewsStyleID = Convert.ToInt32((DetailsView1.FindControl("ddlNewsStyleID") as DropDownList).SelectedValue);
        newTemp.NewsDate = Convert.ToDateTime(DateTime.Now.ToString());
        newTemp.NewsTitle = (DetailsView1.FindControl("txtTitle") as TextBox).Text.Trim();
        newTemp.NewsShort = (DetailsView1.FindControl("RadEditorShort") as RadEditor).Content.Trim();
        newTemp.NewsLong = (DetailsView1.FindControl("RadEditorLong") as RadEditor).Content.Trim();
        string fileUpload = (DetailsView1.FindControl("FileUpload1") as FileUpload).FileName.Trim();
        if (!fileUpload.Equals(""))
        {
            string file = HandBook.Common.FileManager.GetFileName(fileUpload);
            string path = Server.MapPath("~/Images/News/" + file);
            (DetailsView1.FindControl("FileUpload1") as FileUpload).SaveAs(path);
            newTemp.NewsImageName = file;
        }
        newTemp.NewsPath = (DetailsView1.FindControl("txtPath") as TextBox).Text.Trim();
        newTemp.IsShowHomePage = Convert.ToBoolean((DetailsView1.FindControl("ddlIsActivated") as DropDownList).SelectedValue.ToString());
        HandBookData.EatDrinkDB.News.InsertOnSubmit(newTemp);
        HandBookData.EatDrinkDB.SubmitChanges();
    }
}
